import { all, put, takeEvery, takeLatest } from 'redux-saga/effects'
import axios from 'axios'
import { FETCH_HOME_MULTIDATA } from './contants'
import {
  changeBannersAction,
  changeRecommendAction
} from './actionCreators'

function* fetchMultiData(action) {
  const res = yield axios.get('http://123.207.32.32:8000/home/multidata')
  const data = res.data.data
  // yield put(changeBannersAction(data.banner.list))
  // yield put(changeRecommendAction(data.recommend.list))
  yield all([
    yield put(changeBannersAction(data.banner.list)),
    yield put(changeRecommendAction(data.recommend.list))
  ])
}

function* mySaga() {
  // takeLatest takeEvery的区别
  // takeLatest：一次只能监听一个对应的action
  // takeEvery: 每一个都会执行
  // yield takeEvery(FETCH_HOME_MULTIDATA, fetchMultiData)
  yield takeLatest(FETCH_HOME_MULTIDATA, fetchMultiData)
}

export default mySaga